package com.baobao.web;

import com.baobao.dao.ChatDAO;

import com.baobao.domain.Chat;

import com.baobao.service.ChatService;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Chat entities
 * 
 */

@Controller("ChatController")
public class ChatController {

	/**
	 * DAO injected by Spring that manages Chat entities
	 * 
	 */
	@Autowired
	private ChatDAO chatDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Chat entities
	 * 
	 */
	@Autowired
	private ChatService chatService;

	/**
	 * Edit an existing Chat entity
	 * 
	 */
	@RequestMapping("/editChat")
	public ModelAndView editChat(@RequestParam String senderKey, @RequestParam String receiverKey, @RequestParam Calendar dateKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("chat", chatDAO.findChatByPrimaryKey(senderKey, receiverKey, dateKey));
		mav.setViewName("chat/editChat.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Chat entities
	 * 
	 */
	public String indexChat() {
		return "redirect:/indexChat";
	}

	/**
	 * Show all Chat entities
	 * 
	 */
	@RequestMapping("/indexChat")
	public ModelAndView listChats() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("chats", chatService.loadChats());

		mav.setViewName("chat/listChats.jsp");

		return mav;
	}

	/**
	 * Save an existing Chat entity
	 * 
	 */
	@RequestMapping("/saveChat")
	public String saveChat(@ModelAttribute Chat chat) {
		chatService.saveChat(chat);
		return "forward:/indexChat";
	}

	/**
	 * Select the Chat entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteChat")
	public ModelAndView confirmDeleteChat(@RequestParam String senderKey, @RequestParam String receiverKey, @RequestParam Calendar dateKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("chat", chatDAO.findChatByPrimaryKey(senderKey, receiverKey, dateKey));
		mav.setViewName("chat/deleteChat.jsp");

		return mav;
	}

	/**
	 * Create a new Chat entity
	 * 
	 */
	@RequestMapping("/newChat")
	public ModelAndView newChat() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("chat", new Chat());
		mav.addObject("newFlag", true);
		mav.setViewName("chat/editChat.jsp");

		return mav;
	}

	/**
	 * Select an existing Chat entity
	 * 
	 */
	@RequestMapping("/selectChat")
	public ModelAndView selectChat(@RequestParam String senderKey, @RequestParam String receiverKey, @RequestParam Calendar dateKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("chat", chatDAO.findChatByPrimaryKey(senderKey, receiverKey, dateKey));
		mav.setViewName("chat/viewChat.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/chatController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Delete an existing Chat entity
	 * 
	 */
	@RequestMapping("/deleteChat")
	public String deleteChat(@RequestParam String senderKey, @RequestParam String receiverKey, @RequestParam Calendar dateKey) {
		Chat chat = chatDAO.findChatByPrimaryKey(senderKey, receiverKey, dateKey);
		chatService.deleteChat(chat);
		return "forward:/indexChat";
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}
}